/*
Author: winter
Date: Oct. 14, 2003
Purpose: initialize hash table & shm
*/

#include "hhash.h"

/* insert to one hash */
void HH_insert(int num,char *str) {
	
	ENTRY	item;
	//printf("HH:%d-%s\n",num,str);
	item.key = str,
	item.data=(char *) num,
	(void) hsearch(item, ENTER);
}

/* search the hash */
int HH_search(char stockno[6]) {
	
	ENTRY	item, *found_item;
	
	item.key = stockno;
	if ((found_item = hsearch(item, FIND)) != NULL) 
		return	(int) found_item->data;
	else return -1;
}

/* initialize the shm */
void SHM_init(key_t key) {
	
	if(( shmid=shmget(key,shmsize,PERMS|IPC_CREAT)) < 0) //get key 
		lprintf(log, DEBUG,"shmget fatal!!"),exit(1);
	if((ltick=(ltick_t *)shmat(shmid,NULL,0)) == NULL) //attach
		lprintf(log, DEBUG,"shmat fatal!!"),exit(1);
}

/* create the shm */
void SHM_HH_create(int total) {
	
	int	i,istockno;
	char	stockno[6];
  
	for(i=0 ;i < total ; i++ )  
	{
		//printf("ltick_total:%d\n",ltick->total),exit(0);
		istockno=atoi(ltick->istockno[i]),
		sprintf(stockno,"%s",ltick->DIFF_num[istockno].stockno);
		HH_insert(istockno,stockno);
	}
}
